package com.jxnu.duewang.zmly.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.jxnu.duewang.zmly.R;

import liyuxian.fragment.adapter.BradCastAdapter;
import liyuxian.fragment.adapter.LocalRadiosAdapter;
import liyuxian.fragment.bean.BradCastEntity;
import liyuxian.fragment.callback.HttpService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class BroadcastFragment extends BaseFragment implements View.OnClickListener, Callback<BradCastEntity>{

    private BradCastAdapter adapter;
    private GridLayout grid2;
    private GridLayout grid4;
    private ImageView image_view2;
    private ImageView image_view4;
    private RecyclerView recyclerview;
    private RecyclerView bard_recycler;
    private LocalRadiosAdapter locaAdapter;
    public BroadcastFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmnetTitle() {
        return "广播";
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_broadcast, container, false);
        grid2 = (GridLayout) rootView.findViewById(R.id.grid2);
        grid4 = (GridLayout) rootView.findViewById(R.id.grid4);
        image_view2 = (ImageView) rootView.findViewById(R.id.image2);
        image_view4 = (ImageView) rootView.findViewById(R.id.image4);
        image_view2.setOnClickListener(this);
        image_view4.setOnClickListener(this);

        recyclerview = (RecyclerView)rootView.findViewById(R.id.beijin_recycler);
        bard_recycler = (RecyclerView) rootView.findViewById(R.id.bard_recycler);
        adapter = new BradCastAdapter(getContext());
        locaAdapter = new LocalRadiosAdapter(getActivity());
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getData().enqueue(this);
        recyclerview.setAdapter(adapter);
        bard_recycler.setAdapter(locaAdapter);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image2:
                grid4.setVisibility(View.VISIBLE);
                grid2.setVisibility(View.GONE);
                image_view4.setVisibility(View.VISIBLE);
                break;
            case R.id.image4:
                grid2.setVisibility(View.VISIBLE);
                grid4.setVisibility(View.GONE);
                image_view2.setVisibility(View.VISIBLE);
                break;

        }
    }

    @Override
    public void onResponse(Call<BradCastEntity> call, Response<BradCastEntity> response) {
        BradCastEntity body = response.body();
        adapter.addAll(body.getData().getTopRadios());
        locaAdapter.addAll(body.getData().getLocalRadios());
        Log.d("kkk",""+body.getData().getTopRadios().size());
    }

    @Override
    public void onFailure(Call<BradCastEntity> call, Throwable t) {

    }
}

